package com.siwei.mes.common.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.siwei.mes.common.util.InterprectChineseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: mes-platform
 * @description:
 * @author: 作者名字
 * @create: 2024-01-04 23:40
 **/
@Slf4j
@Aspect
@Component
public class DictAspect {
    @Pointcut("execution(* com.siwei.mes.mapper.*.get*(..))") // 指定mapper下所有查询方法
    public void doGet() {
    }

    @Pointcut("execution(* com.siwei.mes.mapper.*.loadById*(..))")
    private void doloadById() {
    }

    @Pointcut("doGet() || doloadById()")
    private void doPointcut() {
    }

    @AfterReturning(pointcut = "doPointcut()", returning = "result")
    public void doAfterReturning(JoinPoint pjp, Object result) throws Exception {
        try {
            if(ObjectUtil.isNotEmpty(result)){
                if (result instanceof List) {
                    List<?> objectList = ((List<?>) result);
                    if (CollectionUtils.isNotEmpty(objectList)) {
                        objectList.forEach(InterprectChineseUtils::reflexEnum);
                    }
                } else {
                    InterprectChineseUtils.reflexEnum(result);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("查询结果字典转换失败" + e.getMessage());
        }
    }

}
